/** ******************************自定义过滤器******************** */

/**
 * 字符串截取并拼接...
 *
 * @param {*} str 字符串
 * @param {*} length  截取长度
 */
export function tipFilter(str, length) {
  if (!str) {
    return ''
  }
  if (str.length <= length) {
    return str
  }
  return str.substring(0, length) + '...'
}

/**
 * 金额格式化为千分位
 *
 * @param {*} v
 */
export function moneyFormat(value) {
  if (value === 0) {
    return '0.00'
  }
  if (value === '0') {
    return '0.00'
  }
  if (!value) {
    //return '0.00'
    return null
  }
  if (isNaN(Number(value))) {
    return '0.00'
  }

  value = Number(value).toFixed(2)
  let b = false
  if (Number(value) < 0) {
    b = true
    value = value + ''
    value = value.substring(1, value.length)
  }
  value = value.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')
  if (b) {
    value = '-' + value
  }
  return value
}

export function moneyFormat3(value) {
  if (value === 0) {
    return '0.00'
  }
  if (value === '0') {
    return '0.000'
  }
  if (!value) {
    //return '0.00'
    return null
  }
  if (isNaN(Number(value))) {
    return '0.000'
  }

  value = Number(value).toFixed(3)
  let b = false
  if (Number(value) < 0) {
    b = true
    value = value + ''
    value = value.substring(1, value.length)
  }
  value = value.replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')
  if (b) {
    value = '-' + value
  }
  return value
}

export function formatterNum({ cellValue }) {
  return XEUtils.commafy(XEUtils.toNumber(cellValue), { digits: 2 })
}

// 计算天数
export function getDay(value) {
  const today = new Date(); //当前时间
  const end_time = Math.ceil(today.getTime() / 1000);
  const start_time = Math.ceil(new Date(value).getTime() / 1000);
  let day = Math.ceil((start_time - end_time) / 86400);
  return day;
}

/**
 * 获取时间选择区间
 * @param  time
 */
export function getDateInterval() {
  // 时间快捷选项
  const pickerOptions = {
    shortcuts: [
      {
        text: '最近一周',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          picker.$emit('pick', [start, end]);
        }
      },
      {
        text: '最近两周',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 14);
          picker.$emit('pick', [start, end]);
        }
      },
      {
        text: '最近三周',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 21);
          picker.$emit('pick', [start, end]);
        }
      },
      {
        text: '最近一个月',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          picker.$emit('pick', [start, end]);
        }
      },
      {
        text: '最近两个月',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 60);
          picker.$emit('pick', [start, end]);
        }
      },
      {
        text: '最近三个月',
        onClick(picker) {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          picker.$emit('pick', [start, end]);
        }
      }]
  }
  return pickerOptions;
}